<template>
  <div class="app-container">
    <div>
      <el-input
        v-model="viewModel.Name"
        size="small"
        placeholder="名称"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="queryList"
      />
      <el-select v-model="viewModel.CategoryId" filterable size="small" placeholder="请选择">
        <el-option
          v-for="item in queryCategoryList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>

      <el-select v-model="viewModel.TypeCode" filterable size="small" placeholder="请选择">
        <el-option
          v-for="item in queryTypeList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-button type="primary" size="small" @click="queryList" icon="el-icon-search">查询</el-button>
      <el-button type="primary" size="small" @click="handleAdd" icon="el-icon-plus">新增</el-button>
      <el-button type="primary" size="small" @click="refresh" icon="el-icon-refresh">刷新</el-button>
      <el-button
        style="margin-left: 10px;"
        size="small"
        type="primary"
        icon="el-icon-upload"
        @click="handleUpload"
      >导入</el-button>
    </div>

    <el-row :gutter="30" v-loading="listLoading">
      <el-col :span="6" v-for="m in modelList" :key="m.ID" style="margin-top: 20px;">
        <el-card :body-style="{ padding: '0px' }">
          <img :src="m.ShowCoverUrl" class="image-c" />
          <div style="padding: 14px;">
            <span
              style="text-overflow:ellipsis !important;white-space:nowrap !important;overflow:hidden !important;width: 100% !important;"
              :title="m.Name"
            >{{ m.Name }}</span>
            <div class="bottom clearfix">
              <span class="time">分类：{{ m.CategoryName}} || 班级分类：{{ m.TypeName}}</span>
              <div style="text-align: right;">
                <!-- <el-button type="text" size="mini" icon="el-icon-scissors" @click="guanlian(m)">关联</el-button>manageChapter -->
                <el-button
                  type="text"
                  size="mini"
                  icon="el-icon-scissors"
                  @click="manageChapter(m)"
                >管理章节</el-button>
                <el-button type="text" size="mini" icon="el-icon-check" @click="handleEdit(m)">编辑</el-button>
                <el-button type="text" size="mini" icon="el-icon-delete" @click="handleDelete(m)">删除</el-button>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 分页组件：https://element.eleme.cn/#/zh-CN/component/pagination -->
    <div class="block" style="float:right;margin-top:15px;" v-if="total>0">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="viewModel.PageIndex"
        :page-sizes="[8, 16, 24, 40,80]"
        :page-size="viewModel.pageSize"
        @prev-click="lastPage"
        @next-click="nextPage"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>

    <el-dialog
      :visible.sync="uploadVisible"
      title="导入课程"
      :close-on-click-modal="false"
      @close="refresh"
      width="75%"
      top="6vh"
    >
      <template>
        <info-upload :dateText="randomNum" ref="upstu"></info-upload>
      </template>
    </el-dialog>

    <el-dialog
      :visible.sync="dialogVisible"
      :title="dialogType==='edit'?'编辑':'新增'"
      @close="closeDialog"
      width="50%"
      top="2vh"
      :close-on-click-modal="false"
    >
      <el-form
        :model="model"
        ref="model"
        :rules="rules"
        label-width="80px"
        label-position="right"
        style="width: 90%;"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="分类" prop="CategoryId">
              <el-select
                v-model="model.CategoryId"
                style="width:100%"
                filterable
                placeholder="请选择分类"
              >
                <el-option
                  v-for="item in categoryList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="班级类型" prop="TypeCode">
              <el-select v-model="model.TypeCode" style="width:100%" placeholder="请选择班级类型">
                <el-option
                  v-for="item in typeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="名称" prop="Name">
              <el-input v-model="model.Name" placeholder="名称" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="封面" prop="ShowCoverUrl">
              <el-upload
                class="avatar-uploader"
                :action="actionUrl"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
              >
                <img v-if="model.ShowCoverUrl" :src="model.ShowCoverUrl" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="试卷" prop="PaperId">
              <el-select
                v-model="model.PaperIds"
                multiple
                filterable
                style="width:100%"
                placeholder="请选择试卷"
              >
                <el-option
                  v-for="item in paperList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="序号" prop="Sort">
              <el-input-number
                v-model="model.Sort"
                style="width:100%"
                :min="0"
                :step="0.5"
                label="序号"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="原价" prop="OldPrice">
              <el-input-number
                v-model="model.OldPrice"
                style="width:100%"
                :min="0"
                :step="0.5"
                label="原价"
              ></el-input-number>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="现价" prop="NowPrice">
              <el-input-number
                v-model="model.NowPrice"
                style="width:100%"
                :min="0"
                :step="0.5"
                label="现价"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="讲师" prop="TeacherId">
              <el-select
                v-model="model.TeacherId"
                filterable
                style="width:100%"
                placeholder="请选择讲师"
              >
                <el-option
                  v-for="item in teacherList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公开课">
              <el-radio-group v-model="isPublic">
                <el-radio
                  border
                  v-for="item in publicList"
                  :label="item.value"
                  :key="item.value"
                >{{item.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="描述" prop="Description">
              <el-input type="textarea" :rows="3" v-model="model.Description" placeholder="描述" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div style="text-align:right;">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" :loading="loading" @click="confirmModel">提交</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :visible.sync="batch"
      :title="'上传视频'"
      width="50%"
      @close="closeBatch"
      :before-close="handleClose"
      top="2vh"
      :close-on-click-modal="false"
    >
      <el-table :data="uploaderInfos" style="width: 100%">
        <el-table-column prop="date" label="视频名称">
          <template slot-scope="scope">{{ scope.row.Name }}</template>
        </el-table-column>
        <el-table-column prop="date" label="上传进度" width="180">
          <template slot-scope="scope">{{ scope.row.progress }}</template>
        </el-table-column>
        <el-table-column prop="date" label="上传结果" width="180">
          <template
            slot-scope="scope"
          >{{ scope.row.isVideoUploadCancel ? '已取消': scope.row.isVideoUploadSuccess ? '上传成功' : '上传中'}}</template>
        </el-table-column>
      </el-table>

      <div style="text-align:right;margin-top:20px;">
        <el-button type="danger" @click="closeVideo">取消</el-button>
        <el-button type="primary" :loading="loading" @click="saveVideo">提交</el-button>
      </div>
    </el-dialog>

    <el-dialog
      width="70%"
      title="管理章节"
      :visible.sync="dialogChapterVisible"
      append-to-body
      :close-on-click-modal="false"
      top="3vh"
    >
      <el-row>
        <el-col :span="10">
          <el-upload
            class="avatar-uploader"
            accept="video/mp4"
            style="float:right;margin-left:20px;"
            :action="actionUrl"
            :show-file-list="false"
            multiple
            :before-upload="beforeUploadForAdd"
          >
            <el-button type="primary" size="small">批量上传</el-button>
          </el-upload>
          <el-button
            style="float:right;"
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="addChapter"
          >添加章节</el-button>
        </el-col>
      </el-row>
      <el-row style="margin-top: 10px;" :gutter="8">
        <el-col :span="10">
          <el-table :data="chapterList" style="width: 100%" border height="400">
            <el-table-column prop="Name" label="名称" show-overflow-tooltip align="center"></el-table-column>
            <el-table-column fixed="right" label="操作" width="100" align="center">
              <template slot-scope="scope">
                <el-button
                  icon="el-icon-edit"
                  @click="editChapter(scope.row)"
                  type="text"
                  title="编辑"
                  size="small"
                ></el-button>
                <el-button
                  icon="el-icon-delete"
                  @click="deleteChapter(scope.row)"
                  type="text"
                  title="删除"
                  size="small"
                ></el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>

        <el-col :span="14">
          <el-form
            :model="chapterModel"
            ref="chapterModel"
            :rules="chapterRules"
            label-width="80px"
            label-position="right"
            style="width: 90%;"
          >
            <el-row>
              <el-col :span="24">
                <el-form-item label="名称" prop="Name">
                  <el-input v-model="chapterModel.Name" placeholder="请输入名称" />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="关联视频" prop="VideoLibId">
                  <el-select
                    v-model="chapterModel.VideoLibId"
                    filterable
                    placeholder="请选择"
                    style="width: 100%;"
                  >
                    <el-option
                      v-for="item in allVideoList"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="关联试卷" prop="PaperId">
                  <el-select
                    v-model="chapterModel.PaperId"
                    filterable
                    placeholder="请选择"
                    style="width: 100%;"
                  >
                    <el-option
                      v-for="item in paperList"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="及格分数" prop="PassGrade">
                  <el-input-number
                    v-model="chapterModel.PassGrade"
                    style="width: 100%;"
                    :step="0.5"
                    :min="0"
                  ></el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="序号" prop="Sort">
                  <el-input-number
                    v-model="chapterModel.Sort"
                    style="width: 100%;"
                    :step="0.5"
                    :min="0"
                  ></el-input-number>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="描述" prop="Description">
                  <el-input
                    type="textarea"
                    :row="5"
                    style="width: 100%;"
                    v-model="chapterModel.Description"
                    placeholder="请输入描述"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div style="text-align:right;margin-top: 40px;">
            <el-button type="danger" @click="dialogChapterVisible=false">关闭</el-button>
            <el-button type="primary" @click="saveChapter">保存</el-button>
          </div>
        </el-col>
      </el-row>
    </el-dialog>

    <el-dialog
      :visible.sync="dialogVideoVisible"
      :title="'关联视频'"
      width="50%"
      top="4vh"
      :close-on-click-modal="false"
    >
      <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
        <el-tab-pane :label="t.label" v-for="t in tabs" :key="t.value" :name="t.value">
          <div>
            <el-input
              v-model="videoViewModel.VideoName"
              size="small"
              placeholder="视频名称"
              style="width: 200px;"
              class="filter-item"
              @keyup.enter.native="queryList"
            />
            <el-button type="primary" size="small" @click="queryVideoList" icon="el-icon-search">查询</el-button>
            <el-button type="primary" size="small" @click="refreshVideo" icon="el-icon-refresh">刷新</el-button>
          </div>
          <el-table
            :data="videoList"
            :header-cell-style="{background:'#eef1f6',color:'#606266'}"
            style="width: 100%;margin-top:30px;"
            border
            v-loading="videoLoading"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column label="名称" prop="Name">
              <template slot-scope="scope">{{ scope.row.Name }}</template>
            </el-table-column>
            <el-table-column label="描述" prop="Description">
              <template slot-scope="scope">{{ scope.row.Description }}</template>
            </el-table-column>

            <el-table-column label="分类名称" prop="CategoryName">
              <template slot-scope="scope">{{ scope.row.CategoryName }}</template>
            </el-table-column>

            <el-table-column label="基础分类" prop="TypeName">
              <template slot-scope="scope">{{ scope.row.TypeName }}</template>
            </el-table-column>

            <el-table-column label="状态" prop="StatusStr">
              <template slot-scope="scope">{{ scope.row.StatusStr }}</template>
            </el-table-column>
          </el-table>
          <!-- 分页组件：https://element.eleme.cn/#/zh-CN/component/pagination -->
          <div class="block" style="float:right;margin-top:15px;" v-if="total>0">
            <el-pagination
              @size-change="handleVideoSizeChange"
              @current-change="handleVideoCurrentChange"
              :current-page="videoViewModel.PageIndex"
              :page-sizes="[10, 20, 30, 40,100]"
              :page-size="videoViewModel.pageSize"
              background
              layout="total, sizes,  pager,  jumper"
              :total="videoTotal"
            ></el-pagination>
          </div>
        </el-tab-pane>
      </el-tabs>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelForm">取 消</el-button>
        <el-button type="primary" @click="saveGuanlian">{{activeName=="1"?"取消关联":"保存关联"}}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 100%;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.v-modal {
  z-index: 2000 !important;
}

.time {
  font-size: 13px;
  color: #999;
}

.bottom {
  margin-top: 13px;
  line-height: 12px;
}

.image-c {
  width: 100%;
  height: 240px;
  display: block;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}
</style>

<script src="@/js/course/index.ts">
</script>
